/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Helpers;

import Hibernate.HibernateUtil;
import Entities.ContentModule;
import java.util.List;
import org.hibernate.*;

/**
 *
 * @author Erhan
 */
public class ContentModuleHelper {

    Session session = null;

    /**
     *
     */
    public ContentModuleHelper() {
        this.session = HibernateUtil.getSessionFactory().getCurrentSession();
    }

    //Ophalen alles
    /**
     *
     * @return
     */
    public List<ContentModule> getcontentModule() {
        List<ContentModule> contentModuleList = null;
        try {
            org.hibernate.Transaction tx = session.beginTransaction();
            Query q = session.createQuery("from Content_Module");
            contentModuleList = (List<ContentModule>) q.list();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return contentModuleList;
    }

    //Ophalen van 1
    /**
     *
     * @param id
     * @return
     */
    public ContentModule getContentModule(int id) {
        ContentModule result = null;
        try {
            org.hibernate.Transaction tx = session.beginTransaction();
            Query q = session.createQuery("from Content_Module where id=" + id);
            result = (ContentModule) q.list().get(0);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return result;
    }


    // Wegschrijven
    /**
     *
     * @param contentModule
     */
    public void persistContentModule(ContentModule contentModule) {
        org.hibernate.Transaction tx = session.beginTransaction();
        tx.begin();
        session.persist(contentModule);
        tx.commit();
    }

    // Verwijderen
    /**
     *
     * @param contentModule
     */
    public void deleteContentModule(ContentModule contentModule) {
        org.hibernate.Transaction tx = session.beginTransaction();
        tx.begin();
        session.delete(contentModule);
        tx.commit();
    }

    // Updaten
    /**
     *
     * @param contentModule
     */
    public void updateContentModule(ContentModule contentModule) {
        org.hibernate.Transaction tx = session.beginTransaction();
        tx.begin();
        session.update(contentModule);
        tx.commit();
    }
}